#include "playscene.h"
#include <QDebug>
#include <QMenuBar>
#include <QMenu>
#include <QAction>
#include <QPainter>
#include <QLabel>
#include "mypushbutton.h"
#include "mycoin.h"
#include <QMap>
#include <QLabel>
#include <QPropertyAnimation>
#include <QSound>

// PlayScene::PlayScene(QWidget *parent)
//     : QMainWindow{parent}
// {}

PlayScene::PlayScene(int levelIndex){
    //记录用户选择的具体关卡号
    this->levelIndex = levelIndex;
    qDebug() << "用户选择的是第" << this->levelIndex << "关";

    //设置标题
    this->setWindowTitle("翻金币游戏场景");
    this->setFixedSize(320,580);
    this->setWindowIcon(QIcon(":/res/Coin0001.png"));

    //创建菜单栏
    QMenuBar * bar = this->menuBar();
    this->setMenuBar(bar);

    QMenu *startMenu = bar->addMenu("菜单");
    QAction *quitAction = startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,this,[=](){this->close();});

    //音效
    QSound *flipSound = new QSound(":/res/ConFlipSound.wav");
    QSound *BackSound = new QSound(":/res/BackButtonSound.wav");
    QSound *winSound = new QSound(":/res/LevelWinSound.wav");


    //返回按钮
    MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    connect(backBtn,&MyPushButton::clicked,this,[=](){

        //延时发送返回信号
        // QTimer::singleShot(200,this,[=](){
        //     emit this->chooseSceneBack();
        // });
        BackSound->play();
        emit this->chooseSceneBack();
    });


    //创建具体用户选择的管卡标签
    QLabel *label =  new QLabel();
    label->setParent(this);

    QString str = QString("level %1").arg(this->levelIndex);
    label->setText(str);

    //设置字体和字号
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);

    label->setGeometry(QRect(30,this->height()-50,120,50));


    dataConfig config;
    //QVector<QVector<int>> levelData = config.getLevelData(this->levelIndex);
    //初始化二维数组
    for(int i = 0; i < 4;i++){
        for (int j = 0; j < 4; ++j) {
            this->gameArray[i][j] = config.mData[this->levelIndex][i][j];
        }
    }

    //胜利图标
    QLabel *winLabel = new QLabel;
    QPixmap tmpPix;
    tmpPix.load(":/res/LevelCompletedDialogBg.png");
    winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
    winLabel->setPixmap(tmpPix);
    winLabel->setParent(this);
    winLabel->move((this->width()-tmpPix.width())*0.5,-tmpPix.height());


    //创建金币背景图
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            //QLabel显示图片
            QLabel *label = new QLabel(this);
            label->setGeometry(0,0,50,50);
            label->setPixmap(QPixmap(":/res/BoardNode.png"));
            //label->setParent(this);
            label->move(57 + i * 50,200+j * 50);

            QString str;
            if(this->gameArray[i][j] == 1){
                str = ":/res/Coin0001.png";
            }else {
                str = ":/res/Coin0008.png";
            }
            //创建金币
            MyCoin * coin = new MyCoin(str);
            coin->setParent(this);
            coin->move(59 + i * 50,204+j * 50);

            //给金币赋值
            coin->posX = i;
            coin->posY = j;
            coin->flag = this->gameArray[i][j];

            //将金币放入二位数组
            coinBtn[i][j] = coin;

            //监听点击按钮信号
            connect(coin,&MyCoin::clicked,this,[=](){

                //播放翻金币音效
                flipSound->play();
                coin->changeFlag();
                //同步二位数组，如果以后有保存的需求，可以还原
                this->gameArray[i][j] = this->gameArray[i][j] == 1 ? 0:1;

                //延时翻转周围金币
                QTimer::singleShot(200,this,[=](){
                    //检测右侧
                    if((coin->posX + 1) <= 3){
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX + 1][coin->posY] = this->gameArray[coin->posX + 1][coin->posY] == 1 ? 0:1;
                    }

                    //检测左侧
                    if((coin->posX - 1) >= 0){
                        coinBtn[coin->posX-1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX - 1][coin->posY] = this->gameArray[coin->posX - 1][coin->posY] == 1 ? 0:1;
                    }

                    //检测上侧
                    if((coin->posY - 1) >= 0){
                        coinBtn[coin->posX][coin->posY - 1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY - 1] = this->gameArray[coin->posX][coin->posY -1] == 1 ? 0:1;
                    }

                    //检测下侧
                    if((coin->posY + 1) <= 3){
                        coinBtn[coin->posX][coin->posY + 1]->changeFlag();
                        this->gameArray[coin->posX][coin->posY + 1] = this->gameArray[coin->posX][coin->posY + 1] == 1 ? 0:1;
                    }

                    //开启可以让金币点击的状态
                    for (int i = 0; i < 4; ++i) {
                        for (int j = 0; j < 4; ++j) {
                            coinBtn[i][j]->isWin == false;
                        }
                    }

                    //检测是否胜利
                    isWin = true;
                    for (int i = 0; i < 4; ++i) {
                        for (int j = 0; j < 4; ++j) {
                            if(coinBtn[i][j]->flag == false){
                                isWin = false;
                                break;
                            }
                        }
                    }
                    if(isWin){
                        //游戏胜利
                        winSound->play();
                        for (int i = 0; i < 4; ++i) {
                            for (int j = 0; j < 4; ++j) {
                                coinBtn[i][j]->isWin = true;

                            }
                        }

                        //将胜利的图片做动画
                        QPropertyAnimation *animation = new QPropertyAnimation(winLabel,"geometry");
                        animation->setDuration(1000);
                        animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                        animation->setEndValue(QRect(winLabel->x(),winLabel->y() + 120,winLabel->width(),winLabel->height()));
                        animation->setEasingCurve(QEasingCurve::OutBounce);
                        animation->start(QAbstractAnimation::DeleteWhenStopped);


                    }
                });

            });

        }
    }
}

void PlayScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");

    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //加载标题
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width() * 0.5,pix.height()*0.5);

    painter.drawPixmap(10,30,pix.width(),pix.height(),pix);


}
